<template>
    <div>
        <el-form ref="storeForm" :model="storeForm" :rules="rules" class="form-container">
            <div class="createPost-main-container">
                <el-form-item prop="agreement_content" style="margin-bottom: 40px;" label-width="100px" label="入驻协议:">
                    <tinymce ref="editor" v-model="storeForm.agreement_content" :height="300" />
                </el-form-item>
                <el-button v-loading="loading" style="margin-left: 10px;" type="primary" @click="submitForm">
                    保存
                </el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
import { get, update } from '@/shop/api/conf'
import Tinymce from '@/components/Tinymce'

const defaultForm = {
    agreement_content: ''
}

export default {
    name: 'StoreConfCommon',
    components: { Tinymce },
    data() {
        const validateRequire = (rule, value, callback) => {
            if (value === '') {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        return {
            storeForm: Object.assign({}, defaultForm),
            rules: {
                agreement_content: [{ validator: validateRequire, message: '商家入驻协议为必填字段', trigger: 'blur' }]
            },
            loading: false
        }
    },
    created() {
        get().then(res => {
            this.storeForm.agreement_content = res.data.conf.agreement_content
            if (this.storeForm.agreement_content !== undefined && this.storeForm.agreement_content !== null && this.storeForm.agreement_content !== '') {
                this.$refs['editor'].setContent(this.storeForm.agreement_content)
            }
        })
    },
    methods: {
        submitForm() {
            this.$refs.storeForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    update(this.storeForm).then(response => {
                        this.$notify({
                            title: '成功',
                            message: '修改成功',
                            type: 'success',
                            duration: 2000
                        })
                        // this.postForm.status = 'published'
                        this.loading = false
                        // this.$router.push('/store/index')
                    }).catch(err => {
                        console.log(err)
                        this.loading = false
                    })
                }
            })
        }
    }
}
</script>

<style>
</style>
